package com.sl.service;

import com.sl.mapper.QuestionOptionMapper;
import com.sl.pojo.QuestionOption;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

@Service
public class QuestionOptionService {
    @Resource
    private QuestionOptionMapper questionOptionMapper;

    public List<QuestionOption> findAll(){
      return   questionOptionMapper.selectAll();
    }
//根据questionId中间表找到。答题表的Option 的ID 的SET集合
    public Set<Integer> findOidByQid(Integer qId){
        Example example = new Example(QuestionOption.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("qId",qId);
        List<QuestionOption> questionOptions = questionOptionMapper.selectByExample(example);
        Set<Integer> oIdSet=new HashSet<>();

        for (QuestionOption questionOption : questionOptions) {
            Integer oId = questionOption.getOId();
            oIdSet.add(oId);
        }
        return oIdSet;

    }

    public void addQuestionOption(QuestionOption questionOption){
         questionOptionMapper.insertSelective(questionOption);
    }
    public void deleted(QuestionOption questionOption){
        questionOptionMapper.deleteByPrimaryKey(questionOption);
    }

    public List<QuestionOption> findByQid(Integer qId){
        Example example = new Example(QuestionOption.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("qId",qId);
        List<QuestionOption> questionOptions = questionOptionMapper.selectByExample(example);
        return questionOptions;
    }
}
